package com.ruoyi.project.ledger.service.impl;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExportUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.ledger.domain.LedgerFile;
import com.ruoyi.project.ledger.mapper.LedgerFileMapper;
import net.sf.jxls.transformer.XLSTransformer;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.ledger.mapper.LedgerSafetyProductionMeetingMapper;
import com.ruoyi.project.ledger.domain.LedgerSafetyProductionMeeting;
import com.ruoyi.project.ledger.service.ILedgerSafetyProductionMeetingService;

/**
 * 安全生产领导小组会议记录台账Service业务层处理
 *
 * @author ruoyi
 * @date 2020-04-14
 */
@Service
public class LedgerSafetyProductionMeetingServiceImpl implements ILedgerSafetyProductionMeetingService
{
    @Autowired
    private LedgerSafetyProductionMeetingMapper ledgerSafetyProductionMeetingMapper;
    @Autowired
    private LedgerFileMapper ledgerFileMapper;
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询安全生产领导小组会议记录台账
     *
     * @param id 安全生产领导小组会议记录台账ID
     * @return 安全生产领导小组会议记录台账
     */
    @Override
    public LedgerSafetyProductionMeeting selectLedgerSafetyProductionMeetingById(Long id)
    {
        LedgerSafetyProductionMeeting meeting = ledgerSafetyProductionMeetingMapper.selectLedgerSafetyProductionMeetingById(id);
        if(meeting != null){
            // 获取图片
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("3");      // 安全生产领导小组会议记录台账
            List<Map<String, Object>> imgList = ledgerFileMapper.selectLedgerImgList(ledgerFile);
            meeting.setImgList(imgList);
        }
        return meeting;
    }

    /**
     * 查询安全生产领导小组会议记录台账列表
     *
     * @param ledgerSafetyProductionMeeting 安全生产领导小组会议记录台账
     * @return 安全生产领导小组会议记录台账
     */
    @Override
    public List<LedgerSafetyProductionMeeting> selectLedgerSafetyProductionMeetingList(LedgerSafetyProductionMeeting ledgerSafetyProductionMeeting)
    {
        return ledgerSafetyProductionMeetingMapper.selectLedgerSafetyProductionMeetingList(ledgerSafetyProductionMeeting);
    }

    /**
     * 新增安全生产领导小组会议记录台账
     *
     * @param ledgerSafetyProductionMeeting 安全生产领导小组会议记录台账
     * @return 结果
     */
    @Override
    public int insertLedgerSafetyProductionMeeting(LedgerSafetyProductionMeeting ledgerSafetyProductionMeeting)
    {
        ledgerSafetyProductionMeeting.setCreateTime(DateUtils.getNowDate());
        int i = ledgerSafetyProductionMeetingMapper.insertLedgerSafetyProductionMeeting(ledgerSafetyProductionMeeting);
        // 添加图片
        if(ledgerSafetyProductionMeeting.getImgs() != null && ledgerSafetyProductionMeeting.getImgs().length > 0){
            for(String path : ledgerSafetyProductionMeeting.getImgs()){
                LedgerFile ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("3");    // 安全生产领导小组会议记录台账
                ledgerFile.setLedgerId(ledgerSafetyProductionMeeting.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerSafetyProductionMeeting.getCreateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
            }
        }
        return i;
    }

    /**
     * 修改安全生产领导小组会议记录台账
     *
     * @param ledgerSafetyProductionMeeting 安全生产领导小组会议记录台账
     * @return 结果
     */
    @Override
    public int updateLedgerSafetyProductionMeeting(LedgerSafetyProductionMeeting ledgerSafetyProductionMeeting)
    {
        ledgerSafetyProductionMeeting.setUpdateTime(DateUtils.getNowDate());
        int i = ledgerSafetyProductionMeetingMapper.updateLedgerSafetyProductionMeeting(ledgerSafetyProductionMeeting);

        // 删除台账图片
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(ledgerSafetyProductionMeeting.getId());
        ledgerFile.setLedgerType("3");      // 安全生产领导小组会议记录台账
        ledgerFileMapper.deleteLedgerImg(ledgerFile);
        if(ledgerSafetyProductionMeeting.getImgs() != null && ledgerSafetyProductionMeeting.getImgs().length > 0){
            for(String path : ledgerSafetyProductionMeeting.getImgs()){
                ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("3");    // 安全生产领导小组会议记录台账
                ledgerFile.setLedgerId(ledgerSafetyProductionMeeting.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerSafetyProductionMeeting.getUpdateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
            }
        }
        return i;
    }

    /**
     * 批量删除安全生产领导小组会议记录台账
     *
     * @param ids 需要删除的安全生产领导小组会议记录台账ID
     * @return 结果
     */
    @Override
    public int deleteLedgerSafetyProductionMeetingByIds(Long[] ids)
    {
        int i = ledgerSafetyProductionMeetingMapper.deleteLedgerSafetyProductionMeetingByIds(ids);
        for(Long id : ids){
            // 删除台账图片
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("3");      // 安全生产领导小组会议记录台账
            ledgerFileMapper.deleteLedgerImg(ledgerFile);
        }
        return i;
    }

    /**
     * 删除安全生产领导小组会议记录台账信息
     *
     * @param id 安全生产领导小组会议记录台账ID
     * @return 结果
     */
    @Override
    public int deleteLedgerSafetyProductionMeetingById(Long id)
    {

        int i = ledgerSafetyProductionMeetingMapper.deleteLedgerSafetyProductionMeetingById(id);

        // 删除台账图片
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(id);
        ledgerFile.setLedgerType("3");      // 安全生产领导小组会议记录台账
        ledgerFileMapper.deleteLedgerImg(ledgerFile);

        return i;
    }

    @Override
    public AjaxResult exportDetail(Long id) {
        LedgerSafetyProductionMeeting meeting = ledgerSafetyProductionMeetingMapper.selectLedgerSafetyProductionMeetingById(id);
        if (meeting == null){
            return AjaxResult.error("当前台账信息不存在");
        }
        // 获取图片
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(id);
        ledgerFile.setLedgerType("3");      // 安全生产领导小组会议记录台账
        List<Map<String, Object>> imgList = ledgerFileMapper.selectLedgerImgList(ledgerFile);
        meeting.setImgList(imgList);

        String tplPath =this.getClass().getResource("/").getPath() +"excel/安全生产领导小组会议记录.xls";
        String fileName = "安全生产领导小组会议记录.xls";

        //fileName = FileUtils.setFileDownloadHeader(request, fileName);

        Map<String, Object> beanParams = new HashMap<String, Object>();
        beanParams.put("meeting",meeting);
        beanParams.put("year", new SimpleDateFormat("yyyy").format(meeting.getMeetingTime()));
        beanParams.put("month", new SimpleDateFormat("MM").format(meeting.getMeetingTime()));
        beanParams.put("day", new SimpleDateFormat("dd").format(meeting.getMeetingTime()));
        beanParams.put("meetingTime", new SimpleDateFormat("HH:mm").format(meeting.getMeetingTime()));
        beanParams.put("meetingWeek", meeting.getMeetingWeek().substring(meeting.getMeetingWeek().length()-1,meeting.getMeetingWeek().length()));

        XLSTransformer former = new XLSTransformer();
        InputStream in = null;
        OutputStream out = null;
        Workbook workbook = null;
        try {
            in = new BufferedInputStream(new FileInputStream(tplPath));
            workbook = former.transformXLS(in, beanParams);
            out = new FileOutputStream(ExcelUtil.getAbsoluteFile(fileName));
            if (meeting.getImgList() != null && meeting.getImgList().size() >0){
                int i = 0;
                int k = 0;
                for (int j=0;j<meeting.getImgList().size();j++){
                    if (j<3){
                        if (StringUtils.isNotBlank(String.valueOf(meeting.getImgList().get(j).get("url")))) {
                            ExportUtil.createPicture(serverConfig.getUrl()+meeting.getImgList().get(j).get("url"), workbook, "Sheet1", (short) (i), 10, (short) (i + 2), 11);
                            i = i + 2;
                        }
                    }else {
                        if (StringUtils.isNotBlank(String.valueOf(meeting.getImgList().get(j).get("url")))){
                            ExportUtil.createPicture(serverConfig.getUrl()+meeting.getImgList().get(j).get("url"),workbook,"Sheet1",(short) (k),11, (short) (k+2), 12);
                        }
                        k=k+2;
                    }
                }
            }
            workbook.write(out);
            return AjaxResult.success(fileName);
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (workbook != null)
            {
                try
                {
                    workbook.close();
                }
                catch (IOException e1)
                {
                    e1.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return AjaxResult.error("导出失败");
    }
}
